//开启抗锯齿
cc.view.enableAntiAlias(true);
(function(){ 
    //提示
    var tipPrefab = null;
    var tipPadding = 20;
    var tipContent = "";

    var defaultSpriteFrame = null;


    var tips_waiting_prefab = null;
    var tips_waiting_clip = null;
    
    var tips_doubleBtn_noTitle_prefab = null;
    var tips_doubleBtn_prefab = null;
    var tips_singleBtn_noTitle_prefab = null;
    var tips_singleBtn_prefab = null;
    var tips_noButton_noTitle = null;

    var FZLTHJW_font = null; 

    var sensitive_font = null;//敏感词字库

    var ROOT_PAGE_TAG = 0;//主界面固定为0
    var PANEL_TAG = 1000;//普通框
    var TIP_TAG = 40000;//提示
    var TIP_LOGIN_TAG = 400001;//等待提示
    
    //等待提示
    var blockNode = null;
    var autoUnblock = true; 
    var timeoutID = 0; 

    //重连提示
    var blockNetNode = null;
    var autoUnNetblock = true;
    var netTimeoutID = 0;
        
    function _chatMsgContentWidget(content, fontsize, width){
        var wordsAmountPerLine = parseInt(width/fontsize);
        //计算有多少行
        var line = 0;
        var lastLineWordAmount = content.length%wordsAmountPerLine;
        if(content.length%wordsAmountPerLine === 0){
            line =  parseInt(content.length/wordsAmountPerLine);  
        }else{
            line = parseInt(content.length/wordsAmountPerLine)+1;
        }
        var container = new cc.Node();
        for(var i = 0; i<line; i++){
            var wordPerLine = "";
            if(i !== (line-1)){
                wordPerLine = content.substring(i*wordsAmountPerLine,(i+1)*wordsAmountPerLine);
            }else{
                wordPerLine = content.substring(i*wordsAmountPerLine,i*wordsAmountPerLine + wordsAmountPerLine);
            }
            var labelNode = new cc.Node();
            var labelPerLine = labelNode.addComponent(cc.Label);
            labelNode.color = cc.Color.BLACK;
            labelPerLine.fontSize = fontsize;
           
            labelPerLine.string = wordPerLine;
             
            labelPerLine.node.anchorY = 1;
            labelPerLine.node.anchorX = 0;

            labelNode.y = labelNode.y - i*fontsize - fontsize/3*i;
            container.addChild(labelNode);
        }
        return container;
    }

    function _showTips(content,delaytime){
        if(tipPrefab == null)
        {
            cc.error("componentsutils tipPrefab null");
            return;
        }

        var deyTime = isNaN(delaytime)?1:delaytime;
        if(content === "" || content == tipContent){
            return;
        }

        var canvas = cc.find("Canvas");
        var tipPfb = cc.instantiate(tipPrefab);
        canvas.addChild(tipPfb);
        
        tipContent = content;
        
        tipPfb.getChildByName("labTip").getComponent(cc.Label).string = content;
        tipPfb.getChildByName("spBg").width = tipPfb.getChildByName("labTip").width + tipPadding;
        tipPfb.getChildByName("spBg").height = tipPfb.getChildByName("labTip").height + tipPadding;
        var callfun = cc.callFunc(function(){
            tipPfb.destroy();
            tipContent = "";
        });

        tipPfb.runAction(cc.sequence(cc.delayTime(deyTime),cc.fadeOut(2), callfun));
    }
    
    function _showTipsByNode(content, parentNode){
        var node = new cc.Node();
 
        var canvas = cc.find("Canvas");
        canvas.addChild(node);

        var nodeForLabel = new cc.Node();
        var label = nodeForLabel.addComponent(cc.Label);
        label.string = content;
        nodeForLabel.color = cc.Color.BLACK;
        label.fontSize = 30;
        node.addChild(nodeForLabel,2);
        
        var nodeForBg = new cc.Node();
        var bg = cc.instantiate(tipPrefab);
        nodeForBg.color = cc.Color.BLACK;
        
        nodeForBg.addChild(bg);
        bg.type = cc.Sprite.Type.SLICED;
        node.addChild(nodeForBg,1);
        bg.width = label.node.width+20;
        node.y = 0;
        node.height = 80;
        var callfun = cc.callFunc(function(){
            node.destroy();
        });
        node.runAction(cc.sequence(cc.fadeOut(2), callfun));
    }
    
    //保证有且只有一个
    function _showLoadingTips(content){
        if(blockNode !== null && blockNode !== NaN)
        {
            _unblock();
        }

        var canvas = cc.find("Canvas");
        if(canvas == null){
            return null;   
        }
        var node = new cc.Node();
        canvas.addChild(node);
        
        var screenSize = cc.view.getVisibleSize();
        
        node.width = screenSize.width;
        node.height = screenSize.height;
        
        //if(content != undefined)
        //{
            // var nodeForLabel = new cc.Node();
            // var label = nodeForLabel.addComponent(cc.Label);
            // label.string = content;
            // nodeForLabel.color = cc.Color.WHITE;
            // label.fontSize = 50;
            // nodeForLabel.width = 70;
            // nodeForLabel.x = -100;
            // node.addChild(nodeForLabel,2);
        //}
        var waitNode = null;
        waitNode = cc.instantiate(tips_waiting_prefab);
        if(waitNode)
        {
            var anim = waitNode.getComponent(cc.Animation);
            anim.play('ani_waiting');
            node.addChild(waitNode);
        }
            
        node.on(cc.Node.EventType.MOUSE_DOWN, function (event) {
           
        }, this);
        node.on(cc.Node.EventType.TOUCH_MOVE, function (event) {
          
        }, this);
        node.on(cc.Node.EventType.TOUCH_START, function (event) {
           
        }.bind(this), this);
        
        blockNode = node;
        autoUnblock = true;

         timeoutID = setTimeout(function(){
             if(autoUnblock)
             {
                cc.log("componentsutils 10s自动取消链接");
                _unblock();
             }    
          },10000);
    }
    
    function _unblock(){
        if(blockNode !== null && blockNode !== NaN)
        {
            autoUnblock = false;
            clearTimeout(timeoutID);
            blockNode.destroy();
            // var deps = cc.loader.getDependsRecursively('prefabs/public_waiting');
            // cc.loader.release(deps);

            blockNode = null;
        }   
    }

    /**
     * prams
     * type：1有两个按钮无标题，2有两个按钮与标题，3有一个按钮无标题，4有一个按钮有标题, 5无按钮无标题
     */ 
    function _showAlertTips(type, contentStr, closeCallBack, titleStr, confirmCallback, confirmBtnStr, cancelBtnStr){
        var tipsNode = null;
        let tipName = '';
        if(type == 1){
            tipsNode = cc.instantiate(tips_doubleBtn_noTitle_prefab);
            tipName = 'prefabs/public_tips_doubleBtn_noTitle';
        }else if(type == 2){
            tipsNode = cc.instantiate(tips_doubleBtn_prefab);
            tipName = 'prefabs/public_tips_doubleBtn';
        }else if(type == 3){
            tipsNode = cc.instantiate(tips_singleBtn_noTitle_prefab);
            tipName = 'prefabs/public_tips_singleBtn_noTitle';
        }else if(type == 4){
            tipsNode = cc.instantiate(tips_singleBtn_prefab);
            tipName = 'prefabs/public_tips_singleBtn';
        }else if(type == 5){
            tipsNode = cc.instantiate(tips_noButton_noTitle);
            tipName = 'prefabs/public_tips_noButton_noTitle';
        }    

        var canvas = cc.find("Canvas");

        canvas.addChild(tipsNode);

        var bg = tipsNode.getChildByName("spBg");
      
        bg.on(cc.Node.EventType.MOUSE_DOWN, function (event) {
            
        }, this);
        bg.on(cc.Node.EventType.TOUCH_MOVE, function (event) {
            
        }, this);

        
        var _closeCallback = function(){
            if(closeCallBack != null ){
                closeCallBack();
                // var deps = cc.loader.getDependsRecursively(tipName);
                // cc.loader.release(deps);
            }
            
            tipsNode.getComponent("Page").backAndRemove();
        }.bind(this);
        var _confirmCallback = function(){
            if(confirmCallback != null){
                confirmCallback();
            }
            tipsNode.getComponent("Page").backAndRemove();
        }.bind(this);

        var content = tipsNode.getChildByName("content");
        var contentLabelNode = content.getChildByName("rtContent");
        if(type == 1){
            content.getChildByName("btnLeft").on("click", _closeCallback, this);
            content.getChildByName("btnRight").on("click", _confirmCallback, this);
            contentLabelNode.getComponent(cc.RichText).string = contentStr.toString();
            if(cancelBtnStr != null){
                content.getChildByName("btnLeft").getChildByName("labDec").getComponent(cc.Label).string = cancelBtnStr;
            }
            if(confirmBtnStr != null){
                content.getChildByName("btnRight").getChildByName("labDec").getComponent(cc.Label).string = confirmBtnStr;
            }
        }else if(type == 2){
            content.getChildByName("btnLeft").on("click", _closeCallback, this);
            content.getChildByName("btnRight").on("click", _confirmCallback, this);
            contentLabelNode.getComponent(cc.RichText).string = contentStr.toString();
            content.getChildByName("labDec").getComponent(cc.Label).string = titleStr.toString();
            if(cancelBtnStr != null){
                content.getChildByName("btnLeft").getChildByName("labDec").getComponent(cc.Label).string = cancelBtnStr;
            }
            if(confirmBtnStr != null){
                content.getChildByName("btnRight").getChildByName("labDec").getComponent(cc.Label).string = confirmBtnStr;
            }
        }else if(type == 3){
            content.getChildByName("btn").on("click", _confirmCallback, this);
            contentLabelNode.getComponent(cc.RichText).string = contentStr.toString();

            if(confirmBtnStr != null){
                content.getChildByName("btnRight").getChildByName("labDec").getComponent(cc.Label).string = confirmBtnStr;
            }
        }else if(type == 4){
            content.getChildByName("btn").on("click", _confirmCallback, this);
            contentLabelNode.getComponent(cc.RichText).string = contentStr.toString();
            content.getChildByName("labDec").getComponent(cc.Label).string = titleStr.toString();
            if(confirmBtnStr != null){
                content.getChildByName("btn").getChildByName("labDec").getComponent(cc.Label).string = confirmBtnStr;
            }
        }else if(type == 5){
            contentLabelNode.getComponent(cc.RichText).string = contentStr.toString();
        }
        
        return tipsNode;
    }

    function _getSensitiveFont(){
        return sensitive_font || [];
    }
    
    function _preloadRes(){   //预加载公共图片
            cc.loader.loadRes("prefabs/public_tips", function (err, prefab) {
               
                tipPrefab = prefab;                    
            });  
            
            // cc.loader.loadRes("textures/public/wait", cc.SpriteFrame, function (err, frame) {
            //     tips_loading_icon = frame;
            // });

         //等待动画
            cc.loader.loadRes("prefabs/public_waiting", function (err, prefab) {
                tips_waiting_prefab = prefab;
            });
       
            //cc.loader.loadRes("textures/public/wait", cc.SpriteAtlas, (err, atlas) => {
                //var spriteFrames = atlas.getSpriteFrames();
                // tips_waiting_clip = cc.AnimationClip.createWithSpriteFrames(spriteFrames, 12);
                // tips_waiting_clip.name = 'waiting';
                // tips_waiting_clip.wrapMode = cc.WrapMode.Loop;
            //});

            // cc.loader.loadRes("textures/public/pop_black_bg", cc.SpriteFrame, function (err, frame) {
            //     tips_loading_bg = frame;
            // });

            cc.loader.loadRes('/text/sensitiveFont', function (error, txt) {
                if(error){ return }
                sensitive_font = txt.split("|");
            }.bind(this));

            //弹出预加载
            cc.loader.loadRes("prefabs/public_tips_doubleBtn_noTitle", function (err, prefab) {
                tips_doubleBtn_noTitle_prefab = prefab;
            });
            cc.loader.loadRes("prefabs/public_tips_doubleBtn", function (err, prefab) {
                tips_doubleBtn_prefab = prefab;
            });
            cc.loader.loadRes("prefabs/public_tips_singleBtn_noTitle", function (err, prefab) {
                tips_singleBtn_noTitle_prefab = prefab;
            });
            cc.loader.loadRes("prefabs/public_tips_singleBtn", function (err, prefab) {
                tips_singleBtn_prefab = prefab;
            });
            cc.loader.loadRes("prefabs/public_tips_noButton_noTitle", function (err, prefab) {
                tips_noButton_noTitle = prefab;
            });

            // //字体库预加载
            // cc.loader.loadRes("font/FZLTHJW", cc.Font, function (err, res) {
            //     FZLTHJW_font = res;
            // }); 
    }

    // function _getFZLTHJWFont(){
    //     return FZLTHJW_font;
    // }
    
    var _ComponentsUtils = {
        chatMsgContentWidget:_chatMsgContentWidget,
        showTips:_showTips,
        preloadRes:_preloadRes,
        showTipsByNode:_showTipsByNode,
        showAlertTips:_showAlertTips,

        getSensitiveFont:_getSensitiveFont,
        //getFZLTHJWFont:_getFZLTHJWFont,

        block:_showLoadingTips,
        unblock:_unblock,
    };
    window.ComponentsUtils = _ComponentsUtils;
} 
)();
